import storage from 'good-storage'
import { STATE_KEY, PLAY_MODE, FAVORITE_KEY } from '@/assets/js/constant'
import { StateProp } from './type'
import { load } from '@/assets/js/array-store'

const initState = {
    song: null, // 选择的歌手信息
    sequenceList: [], // 顺序列表
    playingState: false, // 播放的状态
    fullScreen: false, // 是否全屏播放
    playList: [], // 播放列表
    currentIndex: 0, // 当前播放歌曲的索引
    playMode: PLAY_MODE.sequence, // 播放方式
    favoriteList: load(FAVORITE_KEY) // 收藏歌曲
} as StateProp

const state = (() => {
    const resString = storage.session.get(STATE_KEY) as StateProp
    let state: StateProp
    if (typeof resString === 'undefined') {
        state = initState
        storage.session.set(STATE_KEY, initState)
    } else {
        state = {
            ...resString,
            favoriteList: load(FAVORITE_KEY) // 收藏歌曲
        }
    }
    return state
})()
export default state
